package yuan.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;



public class ConsumerFastStart {

    public static final String brokerList = "192.168.190.129:9092";
    public static final String topic = "topic-demo";
    public static final String groupId = "group.demo";


    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("bootstrap.servers", brokerList);
        properties.put("group.id", groupId);
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
//        consumer.subscribe(Collections.singletonList(topic));
        final List<PartitionInfo> partitionInfos = consumer.partitionsFor(topic);
        for (PartitionInfo partitionInfo : partitionInfos) {
            System.out.println(partitionInfo);
        }
        consumer.assign(Arrays.asList(new TopicPartition(topic, 0)));
        while (true) {

            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record);
                String value = record.value();

            }
        }
    }


    /**
     *
     * @param a1
     * @param a2
     * @return
     * @throws Exception
     */
    public String methsd1(String a1, String a2) throws Exception{


        return "dsfds";


    }

    /**
     *
     * @return
     * @throws Exception
     */
    public String methsd1() throws Exception{


        return "dsfds";


    }



}
